package com.example.healthapp.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.healthapp.R;

import kt_test.model.OxygenUploadRequest;
import kt_test.network.OxygenUploader;
import kt_test.util.JsonLoader;

public class UploadTestFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 使用 activity_test_upload.xml，而不是 fragment_upload_test.xml
        View view = inflater.inflate(R.layout.activity_test_upload, container, false);

        Button button = view.findViewById(R.id.btn_upload_test);
        button.setOnClickListener(this::onClick);

        return view;
    }

    private void onClick(View v) {
        // 如果 JsonLoader 是 Kotlin object 类（单例），需要通过 INSTANCE 访问
        // 否则用静态调用：JsonLoader.loadOxygenData(context)
        OxygenUploadRequest data = JsonLoader.INSTANCE.loadOxygenData(requireContext());

        if (data != null) {
            // 使用 Kotlin 中为 Java 提供的静态方法
            OxygenUploader.uploadDataJava(
                    data,
                    () -> requireActivity().runOnUiThread(() ->
                            Toast.makeText(getContext(), "上传成功", Toast.LENGTH_SHORT).show()
                    ),
                    error -> requireActivity().runOnUiThread(() ->
                            Toast.makeText(getContext(), "上传失败: " + error, Toast.LENGTH_LONG).show()
                    )
            );
        } else {
            Toast.makeText(getContext(), "加载数据失败", Toast.LENGTH_SHORT).show();
        }
    }
}
